/**
 * Copyright (c) 2014-now, secondriver (secondriver@yeah.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package secondriver.jfinal.rack.pugin.sql;

import java.util.LinkedHashMap;

import org.apache.log4j.Logger;

import secondriver.jfinal.rack.kit.RockStrKit;

import com.jfinal.kit.StrKit;

/**
 * Add/Get sql kit
 * 
 * @author ZhangXiao
 * @time 2014年6月27日
 */
public class SqlKit {

	private static final LinkedHashMap<String, String> SQLMAP = new LinkedHashMap<String, String>(
			20);

	private static final String DEFAULT_GROUNP = "default";

	private static Logger logger = Logger.getLogger(SqlKit.class);

	public static void add(String grounp, String id, String sql) {
		if (StrKit.notBlank(grounp, id, sql)) {
			String key = grounp + id;
			if (SQLMAP.containsKey(key)) {
				logger.warn("SqlMap contains [grounp=" + grounp + ",sql=" + id
						+ "] will replace by this handler.");
			}
			SQLMAP.put(key, sql);
		}
	}

	public static void add(String id, String sql) {
		if (StrKit.notBlank(id, sql)) {
			String key = DEFAULT_GROUNP + id;
			if (SQLMAP.containsKey(key)) {
				logger.warn("SqlMap contains [grounp=" + DEFAULT_GROUNP
						+ ",sql=" + id + "] will replace by this handler.");
			}
			SQLMAP.put(key, sql);
		}
	}

	public static String get(String id) {
		if (StrKit.notBlank(id)) {
			String key = DEFAULT_GROUNP + id;
			String sql = SQLMAP.get(key);
			return null == sql ? "" : sql;
		}
		return "";
	}

	public static String get(String grounp, String id) {
		if (StrKit.notBlank(grounp, id)) {
			String key = grounp + id;
			String sql = SQLMAP.get(key);
			return null == sql ? "" : sql;
		}
		return "";
	}

	public static boolean isExist(String grounp, String id) {
		if (RockStrKit.isBlank(grounp, id)) {
			return false;
		}
		return SQLMAP.containsKey(grounp + id);
	}

	public static boolean isExist(String id) {
		if (RockStrKit.isBlank(id)) {
			return false;
		}
		return SQLMAP.containsKey(DEFAULT_GROUNP + id);
	}

	public static int size() {
		return SQLMAP.size();
	}

	public static void clear() {
		if (!SQLMAP.isEmpty()) {
			SQLMAP.clear();
		}
	}

	public static void sqlXmlInfo() {
		System.out.println("************sql.xml(default)************\n");
		System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		System.out.println("<jfinal>\n");
		System.out
				.println(" <grounp id='please set grounp id, if not set default is 'default' .'>\n");
		System.out
				.println("    <sql id='Please set sql id, if not set will cause error.'> your sql sentence. </sql>\n");
		System.out.println("    ...more sql...\n");
		System.out.println(" </grounp>\n");
		System.out.println("  ...more grounp...\n");
		System.out.println("</jfinl>\n");
		System.out.println("************sql.xml(default)************\n");
	}
}
